Conditions | 10 |
Total Lines | 62 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 32 |
CRAP Score | 10 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Bot.retweet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 1 | import Twit from 'twit'; |
|
24 | |||
25 | async retweet(searchParams: Twit.Params): Promise<void> { |
||
26 | 8 | const tweets = await this.search(searchParams); |
|
27 | |||
28 | 6 | if (!Helper.objectExists(tweets) || tweets.length === 0) { |
|
29 | 1 | this.debug('Found no tweets.'); |
|
30 | 1 | return; |
|
31 | } |
||
32 | |||
33 | 5 | this.debug(`Found ${tweets.length} tweet(s).`); |
|
34 | |||
35 | 5 | const retweetedTweets = await this.searchRetweetedTweets(); |
|
36 | |||
37 | 4 | let count = 0; |
|
38 | 4 | for (const tweet of tweets) { |
|
39 | 5 | count++; |
|
40 | |||
41 | 5 | if (!tweet.isReTweetable()) { |
|
42 | 1 | this.debug(`${count}. Tweet with id: '${tweet.rawTweet.id_str}' is not retweetable because: ${tweet.retweetError}.`); |
|
43 | 1 | continue; |
|
44 | } |
||
45 | |||
46 | 4 | this.debug(`${count}. Tweet with id: '${tweet.rawTweet.id_str}' may be retweeted!`); |
|
47 | |||
48 | 4 | if (this.hasAlreadyReTweeted(retweetedTweets, tweet)) { |
|
49 | 1 | continue; |
|
50 | } |
||
51 | |||
52 | 3 | this.debug(`Retweeting tweet with id: '${tweet.rawTweet.id_str}' ...`); |
|
53 | 3 | const result = await this.twit.post('statuses/retweet/:id', { |
|
54 | id: tweet.rawTweet.id_str |
||
55 | }); |
||
56 | |||
57 | 3 | if (result.resp.statusCode === 200) { |
|
58 | 2 | this.debug('Retweeted!'); |
|
59 | 2 | this.debug('Liking ...'); |
|
60 | |||
61 | 2 | const result = await this.twit.post('favorites/create', { |
|
62 | id: tweet.rawTweet.id_str |
||
63 | }); |
||
64 | |||
65 | 2 | if (result.resp.statusCode === 200) { |
|
66 | 1 | this.debug('Liked!'); |
|
67 | } else { |
||
68 | 1 | this.debug('Cannot like.'); |
|
69 | 2 | if (Helper.objectExists(result.resp.statusMessage)) { |
|
70 | 1 | this.debug(result.resp.statusMessage); |
|
71 | } |
||
72 | |||
73 | 1 | throw new Error('Cannot like'); |
|
74 | } |
||
75 | } else { |
||
76 | 1 | this.debug('Cannot retweet.'); |
|
77 | 2 | if (Helper.objectExists(result.resp.statusMessage)) { |
|
78 | 1 | this.debug(result.resp.statusMessage); |
|
79 | } |
||
80 | |||
81 | 1 | throw new Error('Cannot retweet'); |
|
82 | } |
||
83 | } |
||
84 | |||
85 | 2 | this.debug('All done.'); |
|
86 | } |
||
169 | } |